/**
 * ServiceLocator.java
 * Created 28 Sep 2011
 */
package com.fw.timesheet.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fw.timesheet.constant.DefaultValues;
import com.fw.timesheet.service.AssigneeService;
import com.fw.timesheet.service.ClientService;
import com.fw.timesheet.service.EmployeeService;
import com.fw.timesheet.service.GroupAccessService;
import com.fw.timesheet.service.GroupEmployeeService;
import com.fw.timesheet.service.HolidayService;
import com.fw.timesheet.service.ModuleService;
import com.fw.timesheet.service.ProjectService;
import com.fw.timesheet.service.ProjectTaskService;
import com.fw.timesheet.service.TimesheetActivityService;
import com.fw.timesheet.service.TimesheetService;



/**
 * @author sigit <br>
 * @version 1.0 <br>
 * Application : timesheet <br>
 * Copyright (c) fastwire-group 2011 <br>
 * <br>
 */
public class ServiceLocator {

	//This class logger
	private Log log = LogFactory.getLog(ServiceLocator.class);
	
	//Application Context
	private ApplicationContext applicationContext;
	private static ServiceLocator serviceLocator = new ServiceLocator();
	
	private boolean servicesStored = false;
	
	
	private AssigneeService assigneeService;
	private ClientService clientService;
	private EmployeeService employeeService;
	private GroupAccessService groupAccessService;
	private GroupEmployeeService groupEmployeeService;
	private HolidayService holidayService;
	private ModuleService moduleService;
	private ProjectService projectService;
	private ProjectTaskService projectTaskService;
	private TimesheetActivityService timesheetActivityService;
	private TimesheetService timesheetService;
	
		

	private ServiceLocator() {
		// TODO Auto-generated constructor stub
		log.debug("@timesheet> Create serviceLocator object.");
		this.applicationContext = new ClassPathXmlApplicationContext(DefaultValues.APPLICATION_CONTEXT_PATH);
		if (isServicesStored()) {
			//Store all service 
			storeAllServices();
		}
	}
	
	public static ServiceLocator getInstance() {
		if (serviceLocator == null) {
			serviceLocator = new ServiceLocator();
		}
		
		return serviceLocator;
	}

	private void storeAllServices() {
	   this.assigneeService  = (AssigneeService) this.lookupServiceBean(ServiceBeanName.ASSIGNEE_SERVICE);
	   this.clientService  = (ClientService) this.lookupServiceBean(ServiceBeanName.CLIENT_SERVICE);
	   this.employeeService  = (EmployeeService) this.lookupServiceBean(ServiceBeanName.EMPLOYEE_SERVICE);
	   this.groupAccessService  = (GroupAccessService) this.lookupServiceBean(ServiceBeanName.GROUP_ACCESS_SERVICE);
	   this.groupEmployeeService  = (GroupEmployeeService) this.lookupServiceBean(ServiceBeanName.GROUP_EMPLOYEE_SERVICE);
	   this.holidayService  = (HolidayService) this.lookupServiceBean(ServiceBeanName.HOLIDAY_SERVICE);
	   
	   this.moduleService  = (ModuleService) this.lookupServiceBean(ServiceBeanName.MODULE_SERVICE);
	   this.projectService  = (ProjectService) this.lookupServiceBean(ServiceBeanName.PROJECT_SERVICE);
	   this.projectTaskService  = (ProjectTaskService) this.lookupServiceBean(ServiceBeanName.PROJECT_TASK_SERVICE);
	   this.timesheetActivityService  = (TimesheetActivityService) this.lookupServiceBean(ServiceBeanName.TIMESHEET_ACTIVITY_SERVICE);
	   this.timesheetService  = (TimesheetService) this.lookupServiceBean(ServiceBeanName.TIMESHEET_SERVICE);
   
	   
	}
	
	/**
	 * Lookup service based on service bean name.
	 * refer to ServiceBeanNames class
	 * 
	 * @param serviceBeanName
	 *            the service bean name
	 * @return the service bean
	 */
	public Object lookupServiceBean(String serviceBeanName) {
		return applicationContext.getBean(serviceBeanName);
	}
	
	
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public AssigneeService getAssigneeService() {
		if (isServicesStored()) {
			return assigneeService;
		} else {
			return (AssigneeService) lookupServiceBean(ServiceBeanName.ASSIGNEE_SERVICE);			
		}
		
	}

	public void setAssigneeService(AssigneeService assigneeService) {
		this.assigneeService = assigneeService;
	}
	
	public ClientService getClientService() {
		if (isServicesStored()) {
			return clientService;
		} else {
			return (ClientService) lookupServiceBean(ServiceBeanName.CLIENT_SERVICE);			
		}
		
	}

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}
	

	public EmployeeService getEmployeeService() {
		if (isServicesStored()) {
			return employeeService;
		} else {
			return (EmployeeService) lookupServiceBean(ServiceBeanName.EMPLOYEE_SERVICE);			
		}
		
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public GroupAccessService getGroupAccessService() {
		if (isServicesStored()) {
			return groupAccessService;
		} else {
			return (GroupAccessService) lookupServiceBean(ServiceBeanName.GROUP_ACCESS_SERVICE);			
		}
		
	}

	public void setGroupAccessService(GroupAccessService groupAccessService) {
		this.groupAccessService = groupAccessService;
	}
	

	public GroupEmployeeService getGroupEmployeeService() {
		if (isServicesStored()) {
			return groupEmployeeService;
		} else {
			return (GroupEmployeeService) lookupServiceBean(ServiceBeanName.GROUP_EMPLOYEE_SERVICE);			
		}
		
	}

	public void setGroupEmployeeService(GroupEmployeeService groupEmployeeService) {
		this.groupEmployeeService = groupEmployeeService;
	}
	
	
	public HolidayService getHolidayService() {
		if (isServicesStored()) {
			return holidayService;
		} else {
			return (HolidayService) lookupServiceBean(ServiceBeanName.HOLIDAY_SERVICE);			
		}
		
	}

	public void setHolidayService(HolidayService holidayService) {
		this.holidayService = holidayService;
	}
	

	public ModuleService getModuleService() {
		if (isServicesStored()) {
			return moduleService;
		} else {
			return (ModuleService) lookupServiceBean(ServiceBeanName.MODULE_SERVICE);			
		}
		
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public ProjectService getProjectService() {
		if (isServicesStored()) {
			return projectService;
		} else {
			return (ProjectService) lookupServiceBean(ServiceBeanName.PROJECT_SERVICE);			
		}
		
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public ProjectTaskService getProjectTaskService() {
		if (isServicesStored()) {
			return projectTaskService;
		} else {
			return (ProjectTaskService) lookupServiceBean(ServiceBeanName.PROJECT_TASK_SERVICE);			
		}
		
	}

	public void setProjectTaskService(ProjectTaskService projectTaskService) {
		this.projectTaskService = projectTaskService;
	}

	public TimesheetActivityService getTimesheetActivityService() {
		if (isServicesStored()) {
			return timesheetActivityService;
		} else {
			return (TimesheetActivityService) lookupServiceBean(ServiceBeanName.TIMESHEET_ACTIVITY_SERVICE);			
		}
		
	}

	public void setTimesheetActivityService(TimesheetActivityService timesheetActivityService) {
		this.timesheetActivityService = timesheetActivityService;
	}

	public TimesheetService getTimesheetService() {
		if (isServicesStored()) {
			return timesheetService;
		} else {
			return (TimesheetService) lookupServiceBean(ServiceBeanName.TIMESHEET_SERVICE);			
		}
		
	}

	public void setTimesheetService(TimesheetService timesheetService) {
		this.timesheetService = timesheetService;
	}
	
	
	public boolean isServicesStored() {
		return servicesStored;
	}

	public void setServiceStored(boolean servicesStored) {
		if (!this.servicesStored && servicesStored)
			storeAllServices();
		this.servicesStored = servicesStored;
	}
	
	
	
}
